package com.cloudptt.api.product.func.addr;

import com.cloudptt.api.pjsip.api.SipManager;
import com.cloudptt.api.product.func.IBroadcast;
import com.cloudptt.api.product.func.message.NotifyNewGroupListener;
import com.cloudptt.api.product.log.Log;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class AddrBroadcast extends BroadcastReceiver implements IBroadcast
{
	private Context ctx = null;
	private NotifyNewGroupListener newGroupListener = null;
	private CallBack callback = null;

	public AddrBroadcast(Context ctx,CallBack callback)
	{
		this.ctx = ctx;
		this.callback = callback;
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_MSG_SYS_NOTIFY_ADDR));
	}

	public void setNewGroupListener(NotifyNewGroupListener newGroupListener) {
		this.newGroupListener = newGroupListener;
	}
	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		Log.d(AddrFunc.Log_Key, "Action is " + action);

		if(action.equals(SipManager.ACTION_SIP_MSG_SYS_NOTIFY_ADDR))
		{
			String str = intent.getStringExtra(SipManager.Notify_Str);
			Log.d(AddrFunc.Log_Key, "ACTION_SIP_MSG_SYS_NOTIFY_ADDR str = " + str);
//			SysNotifyBodyInfo info = new SysNotifyBodyInfo(bodyStr);
			if(str != null)
			{
				if(str.equals("UserProfileChange"))
				{
					if(callback != null)
					{
						callback.loadAddress();
					}
				}
				
			}
		}
	}

	public void clearResource()
	{
		Log.d(AddrFunc.Log_Key, "clearResource");
		this.ctx.unregisterReceiver( this );
		this.ctx = null;
	}
}
